package cn.kawa.basic.service.impl;

import cn.kawa.basic.mapper.BaseMapper;
import cn.kawa.basic.query.BaseQuery;
import cn.kawa.basic.service.IBaseService;
import cn.kawa.basic.util.PageList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)

public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {
        return baseMapper.loadAll();
    }

    @Override
    public T findById(Long id) {
        return baseMapper.loadById(id);
    }
    @Transactional
    @Override
    public void add(T t) {
        baseMapper.save(t);

        //update(t);
    }

    @Transactional
    @Override
    public void update(T t) {

        baseMapper.update(t);
    }


    @Transactional
    @Override
    public void delete(Long id) {
        baseMapper.remove(id);
    }

    @Override//分页查询数据:   注意此处的BaseQuery是多态写法，内容为TQuery
    public PageList<T> queryPage(BaseQuery query) {
        //查询总数量
        Integer count = baseMapper.queryCount(query);
        //如果为空就返回空集合
        if(count ==null|| count==0){
            return new PageList<>();
        }
        //不为空就查询list，并和count放入PageList对象里返回
        List<T> ts = baseMapper.queryData(query);
        return new PageList<>(count,ts);
    }

    @Override
    @Transactional
    public void patchDel(List<Long> ids) {
        baseMapper.patchDel(ids);
    }




}
